﻿using System;
using System.Collections.Generic;
using JTLeigh.Commerce.Spencer.Core.Common;

namespace JTLeigh.Commerce.Spencer.Features.ProductImages.Common.Entities
{

	/// <summary>
	/// Wraps a product image and an optional localization, and exposes properties from these 
	/// objects with fallback when the objects are <see langword="null"/>.
	/// </summary>
	public class LocalizedProductImage
	{

		#region Fields

		private ProductImage _image;
		private ProductImageLocalization _localization;

		#endregion // Fields

		#region Constructors

		/// <summary>
		/// Initializes a new instance of the <see cref="LocalizedProductImage"/> class with the
		/// specified size and localization.
		/// </summary>
		/// <param name="image">The <see cref="ProductImage"/> to wrap.</param>
		/// <param name="localization">The <see cref="ProductImageLocalization"/> to wrap, or
		/// <see langword="null"/> if no localization was found.</param>
		public LocalizedProductImage(ProductImage image, ProductImageLocalization localization)
		{
			Safety.AssertArgumentNotNull("image", image);
			// localization can be null

			_image = image;
			_localization = localization;
		}

		#endregion // Constructors

		#region Properties

		/// <summary>
		/// Gets the image identifier.
		/// </summary>
		/// <value>An image identifier <see cref="Guid"/>.</value>
		public Guid ImageId
		{
			get { return _image.ImageId; }
		}

		/// <summary>
		/// Gets the view identifier.
		/// </summary>
		/// <value>A view identifier <see cref="Guid"/>.</value>
		public Guid ViewId
		{
			get { return _image.ViewId; }
		}

		/// <summary>
		/// Gets or sets the alternative text that should be displayed in place of the image.
		/// </summary>
		/// <value>If a localization was provided, the localization's alternative text.  Otherwise, 
		/// the image's default alternative text.</value>
		public string AlternativeText
		{
			get
			{
				return (_localization != null) ? _localization.AlternativeText : _image.DefaultAlternativeText;
			}
		}

		/// <summary>
		/// Gets the usage permission text that should be attached to the image.
		/// </summary>
		/// <value>If a localization was provided, the localization's usage permission text.  
		/// Otherwise, the image's default usage permission text.</value>
		public string UsagePermissionText
		{
			get
			{
				return (_localization != null) ? _localization.UsagePermissionText : _image.DefaultUsagePermissionText;
			}
		}

		/// <summary>
		/// Gets the dictionary of the image's variants.
		/// </summary>
		/// <value>A <see cref="Dictionary{TKey,TValue}"/> of <see cref="ProductImageVariant"/>
		/// keyed by variant identifier <see cref="Guid"/>.</value>
		public Dictionary<Guid, ProductImageVariant> Variants
		{
			get { return _image.Variants; }
		}

		#endregion // Properties

	}

}
